package cn.iocoder.yudao.module.system.service.scorelineconfig;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.system.controller.admin.scorelineconfig.vo.ScoreLineConfigPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.scorelineconfig.vo.ScoreLineConfigSaveReqVO;
import cn.iocoder.yudao.module.system.convert.scorelineconfig.ScoreLineConfigConvert;
import cn.iocoder.yudao.module.system.dal.dataobject.scorelineconfig.ScoreLineConfigDO;
import cn.iocoder.yudao.module.system.dal.mysql.scorelineconfig.ScoreLineConfigMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SCORE_LINE_CONFIG_NOT_EXISTS;

/**
 * 分数线配置 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
@Slf4j
public class ScoreLineConfigServiceImpl implements ScoreLineConfigService {

    @Resource
    private ScoreLineConfigMapper scoreLineConfigMapper;

    @Override
    @CacheEvict(value = "scoreLineConfig", allEntries = true)
    public Long createScoreLineConfig(ScoreLineConfigSaveReqVO createReqVO) {
        // 插入
        ScoreLineConfigDO scoreLineConfig = ScoreLineConfigConvert.INSTANCE.convert(createReqVO);
        scoreLineConfigMapper.insert(scoreLineConfig);
        // 返回
        return scoreLineConfig.getId();
    }

    @Override
    @CacheEvict(value = "scoreLineConfig", allEntries = true)
    public void updateScoreLineConfig(ScoreLineConfigSaveReqVO updateReqVO) {
        // 校验存在
        validateScoreLineConfigExists(updateReqVO.getId());
        // 更新
        ScoreLineConfigDO updateObj = ScoreLineConfigConvert.INSTANCE.convert(updateReqVO);
        scoreLineConfigMapper.updateById(updateObj);
    }

    @Override
    @CacheEvict(value = "scoreLineConfig", allEntries = true)
    public void deleteScoreLineConfig(Long id) {
        // 校验存在
        validateScoreLineConfigExists(id);
        // 删除
        scoreLineConfigMapper.deleteById(id);
    }

    private void validateScoreLineConfigExists(Long id) {
        if (scoreLineConfigMapper.selectById(id) == null) {
            throw exception(SCORE_LINE_CONFIG_NOT_EXISTS);
        }
    }

    @Override
    public ScoreLineConfigDO getScoreLineConfig(Long id) {
        return scoreLineConfigMapper.selectById(id);
    }

    @Override
    public PageResult<ScoreLineConfigDO> getScoreLineConfigPage(ScoreLineConfigPageReqVO pageReqVO) {
        return scoreLineConfigMapper.selectPage(pageReqVO);
    }

    @Override
    @Cacheable(value = "scoreLineConfig", key = "'score:' + #configKey + ':' + #subjectType + ':' + #province + ':' + #year")
    public Integer getScoreByConfigKeyAndSubjectTypeAndProvinceAndYear(String configKey, String subjectType, String province, Integer year) {
        ScoreLineConfigDO config = scoreLineConfigMapper.selectByConfigKeyAndSubjectTypeAndProvinceAndYear(configKey, subjectType, province, year);
        return config != null ? config.getScoreValue() : null;
    }

    @Override
    @Cacheable(value = "scoreLineConfig", key = "'score:' + #configKey + ':' + #subjectType")
    public Integer getScoreByConfigKeyAndSubjectType(String configKey, String subjectType) {
        return getScoreByConfigKeyAndSubjectTypeAndProvinceAndYear(configKey, subjectType, "安徽", 2025);
    }

    @Override
    @Cacheable(value = "scoreLineConfig", key = "'subjectType:' + #subjectType + ':' + #province + ':' + #year")
    public List<ScoreLineConfigDO> getScoreLinesBySubjectTypeAndProvinceAndYear(String subjectType, String province, Integer year) {
        return scoreLineConfigMapper.selectBySubjectTypeAndProvinceAndYear(subjectType, province, year);
    }

    @Override
    @Cacheable(value = "scoreLineConfig", key = "'subjectType:' + #subjectType")
    public List<ScoreLineConfigDO> getScoreLinesBySubjectType(String subjectType) {
        return getScoreLinesBySubjectTypeAndProvinceAndYear(subjectType, "安徽", 2025);
    }

    @Override
    @Cacheable(value = "scoreLineConfig", key = "'allEnabledGroupByType'")
    public Map<String, List<ScoreLineConfigDO>> getAllEnabledScoreLinesGroupByType() {
        List<ScoreLineConfigDO> allConfigs = scoreLineConfigMapper.selectAllEnabled();
        return allConfigs.stream().collect(Collectors.groupingBy(ScoreLineConfigDO::getConfigType));
    }

    @Override
    @Cacheable(value = "scoreLineConfig", key = "'configType:' + #configType")
    public List<ScoreLineConfigDO> getScoreLinesByConfigType(String configType) {
        return scoreLineConfigMapper.selectByConfigType(configType);
    }

    @Override
    @Cacheable(value = "scoreLineConfig", key = "'configTypeScore:' + #configType + ':' + #subjectType")
    public Integer getScoreByConfigType(String configType, String subjectType) {
        return getScoreByConfigTypeAndSubjectTypeAndProvinceAndYear(configType, subjectType, "安徽", 2025);
    }

    @Override
    @Cacheable(value = "scoreLineConfig", key = "'configTypeScore:' + #configType + ':' + #subjectType + ':' + #province + ':' + #year")
    public Integer getScoreByConfigTypeAndSubjectTypeAndProvinceAndYear(String configType, String subjectType, String province, Integer year) {
        List<ScoreLineConfigDO> configs = scoreLineConfigMapper.selectByConfigTypeAndSubjectTypeAndProvinceAndYear(configType, subjectType, province, year);
        return configs.isEmpty() ? null : configs.get(0).getScoreValue();
    }

    @Override
    @CacheEvict(value = "scoreLineConfig", allEntries = true)
    public void refreshScoreLineCache() {
        log.info("分数线配置缓存已刷新");
    }

}